#include"List.h"
void ListPrint(LNode* phead)
{
    assert(phead);
    LNode* cur = phead->next;
    while(cur != phead)
    {
        printf("%d ", cur->val);
        cur = cur->next;
    }
    printf("\n");
}
LNode* BuyListNode(LDataType x)
{
    LNode* newNode = (LNode*)malloc(sizeof(LNode));
    newNode->val = x;
    newNode->next = NULL;
    newNode->prev = NULL;
}
//这里有点小问题  但是问题不大
LNode* ListInit(LNode* phead)
{
    assert(phead);
    //要一个哨兵位
    phead = BuyListNode(-1);
    phead->next = phead;
    phead->prev = phead;
    return phead;
}
void ListPushBack(LNode* phead, LDataType x)
{
    // assert(phead);
    // LNode* tail = phead->prev;
    // LNode* newnode = BuyListNode(x);
    // tail->next = newnode;
    // newnode->prev = tail;
    // newnode->next = phead;
    // phead->prev = newnode;
    ListInsert(phead, phead, x);
}
void ListPushFront(LNode* phead, LDataType x)
{
    //  assert(phead);
    // LNode* newnode = BuyListNode(x);
    // newnode->next = phead->next;
    // newnode->prev = phead;
    // phead->next->prev = newnode;
    // phead->next = newnode;
    ListInsert(phead, phead->next, x);
}
void ListPopBack(LNode* phead)
{
    assert(phead);
    assert(!ListEmpty(phead));
    // LNode* tail = phead->prev;
    // tail->prev->next = phead;
    // phead->prev = tail->prev;
    // free(tail);
    ListErase(phead, phead->prev);

}
void ListPopFront(LNode* phead)
{
     assert(phead);
    assert(!ListEmpty(phead));
    // LNode* del = phead->next;
    // del->next->prev = phead;
    // phead->next = del->next;
    // free(del);
    ListErase(phead, phead->next);
}
//pos位置之前插入
void ListInsert(LNode* phead,LNode* pos, LDataType x)
{
    LNode* newnode = BuyListNode(x);
    newnode->prev = pos->prev;
    newnode->next = pos;
    pos->prev->next =newnode;
    pos->prev = newnode;
}
//pos位置删除
void ListErase(LNode* phead, LNode* pos)
{
    pos->prev->next = pos->next;
    pos->next->prev = pos->prev;
    free(pos);
}
bool ListEmpty(LNode* phead) {return phead->next == phead;}
void ListDestory(LNode* phead)
{
    assert(phead);
    LNode* del = phead;
    while(del != phead)
    {
        LNode* next = del->next;
        free(del);
        del = next;
    }
    free(phead);
}

